
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  poolint.push.tablename;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Vector;

import control.RequestParameter;

import dbmanager.DBManager;
import dbmanager.GetIdName;

public abstract class IntegrateSubPool implements IntegratePool
{
	protected boolean FLAG = false;
	protected String master = null;
	String request;
	String process = "";
	protected GetIdName gin ;
	protected String recentTableObject = "";
	protected String tableObj = "";
	protected String masterObj;
	//protected String uniqueKey = "";
	protected LinkedList<Object> uniqueKeyList = new LinkedList<Object>();
	protected LinkedList<Object> fieldNameList = new LinkedList<Object>();
	
	protected Hashtable<Object, Object> poolHash = new Hashtable<Object, Object>();
	protected Hashtable<Object, Object> fieldHash = new Hashtable<Object, Object>();
	protected Hashtable<Object, Object> ModuleHash = new Hashtable<Object, Object>();
	RequestParameter rqp;
	Vector<Object> oldPropertyVect = new Vector<Object>();
	Vector<Object> propertyVect = new Vector<Object>();
	
	public Vector<Object> poolVector = new Vector<Object>();
	public Vector<Object> uniquePoolVector = new Vector<Object>();
	
	public IntegrateSubPool() {
		gin = new GetIdName();
	}
	public void setTableObj(String tableObj) 
	{
		this.tableObj = tableObj;		
	}
	
	public void setRecentTableObject(String recentTableObject) 
	{
		this.recentTableObject = recentTableObject;
	}
	
	public void setUniqueKeyList(LinkedList<Object> uniqueKeyList) 
	{
		this.uniqueKeyList = uniqueKeyList;
	}
	
	// It will returns the field from fieldObject for the specific TableObject.
//	private String getFieldname(String master, String key) 
	public LinkedList<Object> getFieldname()
	{
		ResultSet rsPoolValue;
		/*rsPoolValue = dbsql.getSelect("SELECT * FROM "+masterTable+"  WHERE id = " + master);
		rsPoolValue.next();
		fieldname=master;			*/
		
		/**SELECT p4.pv FROM property_details p1 JOIN (property_details p2 JOIN (property_details p3 JOIN property_details p4 ON (p3.mid = p4.mid)) ON (p2.pv = p3.mid)) ON (p1.pv = p2.mid)
		where p1.pid = 26 and p1.mid = '9145' and p2.pid = '27' and p3.pid = '8290' and p3.pv = '9008' and p4.pid = '27'*/
		
		String fieldListId = gin.getId("fieldlist");
		String columnId = gin.getId("column");
		String fieldId = gin.getId("field");
		
		/*if(uniqueKey == "")
		{
			uniqueKey = key;
		}*/
		
		
		for (int i = 0; i < uniqueKeyList.size(); i++) 
		{
			String uniqueKey = null; 
			uniqueKey = uniqueKeyList.get(i).toString();		
		
			String query = "SELECT p4.pv as field FROM property_details p1 JOIN (property_details p2 JOIN (property_details p3 JOIN property_details p4 ON (p3.mid = p4.mid)) ON (p2.pv = p3.mid)) ON (p1.pv = p2.mid)"+
							" WHERE p1.pid = " + fieldListId + " and p1.mid = " + tableObj + " and p2.pid = " + fieldId + " and p3.pid = " + columnId+
							" and p3.pv = " + uniqueKey + " and p4.pid = " + fieldId;
			
			rsPoolValue = DBManager.getSelect(query);
			try {
				rsPoolValue.next();
				fieldNameList.add(rsPoolValue.getString("field"));
				rsPoolValue.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			catch (Exception e) { }
		}
		return fieldNameList;
	}
	
	/*public void setUniqueKey(String unique)
	{
		uniqueKey = unique;
	}*/
	
	public void setRequestParam() 
	{
		rqp = new RequestParameter();
		rqp.setUsername("+911234567891");
		rqp.setPassword("891");
		rqp.setProcess(process);
		rqp.setVersion("11");
		rqp.setMessage("");
		rqp.setRequest(request);
		rqp.setEvent(false);
	}
	
	public void setPropertyMaster(String propertyMaster) 
	{
		master = propertyMaster;
	}
	
	
	public void setProcess(String process)
	{
		this.process = process;
	}
	public void setPoolHash(Hashtable<Object, Object> poolHash2) 
	{
		poolHash.putAll(poolHash2);
	}
	
	public void setModuleHash(Hashtable<Object, Object> poolHash2) 
	{
		ModuleHash.putAll(poolHash2);
	}
	
	public void setFieldHash(Hashtable<Object, Object> poolHash2) 
	{
		fieldHash.putAll(poolHash2);
	}

	public Hashtable<Object, Object> getFieldHash() {
		// TODO Auto-generated method stub
		return null;
	}


	public Hashtable<Object, Object> getModuleHash() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String[] getColumnArray(String tablename)
	{
		
		String [] columnArray ;
		
		String query = "select fieldname from command where msgtype = '"+tablename+"' and process = '"+process+"'";
		
		ResultSet rsPoolValue = DBManager.getSelect(query);
		int i = 0; int numberOfColumns=0;
		 
		 try {
			 ResultSetMetaData rsmd = rsPoolValue.getMetaData();
			 numberOfColumns= rsmd.getColumnCount();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	     columnArray = new String[numberOfColumns];
		try
		{						
			while(rsPoolValue.next())
			{
				columnArray[i] = rsPoolValue.getString(1);		
				i++;	
			}
			rsPoolValue.close();
		}
		catch (Exception e) 
		{
			System.out.println("Exception::Integration:: PoolConnection.java, while fetching Fields Values from POOL ");
			e.printStackTrace();
		}
		return columnArray;
	}
	
	public void setMaster(String masterObj) 
	{
		this.masterObj = masterObj;		
	}
	public boolean getResult() {
		return false;
	}
	
	public Vector<Object> getPoolVector()
	{
		return poolVector;
	}
	public Vector<Object> getUniquePoolVector()
	{
		return uniquePoolVector;
	}
}
